using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Windows.Forms;
using System.Drawing;
using OpenComposite.Workflow.Interfaces;
using System.ComponentModel.Design;
using System.Drawing.Design;

namespace OpenComposite.Workflow
{
    [ActivityValidator(typeof(ConditionalBranchValidator))]
	[Designer(typeof(ConditionalBranchDesigner))]
    public partial class ConditionalBranchActivity : HorizontalSequenceActivity
    {

        public static readonly DependencyProperty RuleProperty = DependencyProperty.Register("Rule", typeof(ActivityCondition), typeof(ConditionalBranchActivity));

        [DescriptionAttribute("Condition")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public ActivityCondition Rule
        {
            get
            {
                ActivityCondition activity = (base.GetValue(RuleProperty)) as ActivityCondition;
                if (activity == null) {
                    object o = new object();
                    activity = o as ActivityCondition;
                }
                return activity;
            }
            set { base.SetValue(RuleProperty, value); }
        }
    }

	public class ConditionalBranchDesigner : HorizontalSequenceDesigner
	{
		protected override Pen ConnectorPen
		{
			get
			{
				return new Pen(Color.DarkGreen);
			}
		}

		protected override void OnPaint(ActivityDesignerPaintEventArgs e)
		{
			base.OnPaint(e);
			if (!IsVisible)
				return;

			/* Name (RES) oder Description (IF Rule = RES) */
			//e.Graphics.DrawString(this.Activity.Description, e.DesignerTheme.Font,
			//    e.DesignerTheme.ForegroundBrush, new Point(Bounds.X, Bounds.Top + 5));
		}

		protected override void OnLayoutPosition(ActivityDesignerLayoutEventArgs e)
		{
			base.OnLayoutPosition(e);
			foreach ( ActivityDesigner designer in this.ContainedDesigners ) {
				designer.Location += new Size(50, 0);
			}
		}

		protected override Size OnLayoutSize(ActivityDesignerLayoutEventArgs e)
		{
			Size size = base.OnLayoutSize(e);
			size.Width += 90;
			return size;

		}
		
		public override string Text
		{
			get { return ( (ITextActivity)this.Activity ).Text; }
			protected set { ( (ITextActivity)this.Activity ).Text = value; }
		}

	}

	public partial class ConditionalBranchValidator : ActivityValidator
	{
		public override ValidationErrorCollection Validate(ValidationManager manager, object obj)
		{
			ValidationErrorCollection errors = new ValidationErrorCollection();//errors = base.Validate(manager, obj);
			
			if (obj is ConditionalBranchActivity) {
				ConditionalBranchActivity activity = obj as ConditionalBranchActivity;
				if (activity.Rule == null) {
					// else branch (last) can be be empty
					if ( activity.Parent != null &&
						 activity != activity.Parent.Activities[activity.Parent.Activities.Count - 1]) {
						errors.Add(new ValidationError("Missing Condition", 0, false, "Rule"));
					}
				}
			}
			return errors;
		}
	}
}
